package m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import sun.plugin2.message.Message;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/*发布订阅模式*/
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接
        ConnectionFactory f = new   ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");

        //建立信道
        Channel c = f.newConnection().createChannel();

        //定义交换机
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        //向交换机发送消息
        while(true){
            System.out.println("输入消息：");
            String s = new Scanner(System.in).nextLine();
            //第二个变量是向交换机发消息 而不是向队列发消息
            c.basicPublish("logs","",null, s.getBytes());
        }
    }

}
